package cn.itcast.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: taft
 * @Date: 2018-9-27 11:55
 */
@Component
public class LoginFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //获取当前请求的上下文
        RequestContext currentContext = RequestContext.getCurrentContext();

        //获取请求对象
        HttpServletRequest request = currentContext.getRequest();

        String paizi = request.getParameter("paizi");

        //没有牌子不让进
        if (null==paizi|| StringUtils.isEmpty(paizi)){
            //不响应数据
            currentContext.setSendZuulResponse(false);

            //返回状态码
            currentContext.setResponseStatusCode(401);
        }

        return null;
    }
}
